package cn.zn.controller.system;

import cn.zn.base.annotation.Log;
import cn.zn.base.bean.dto.ResultData;
import cn.zn.base.common.enums.BusinessType;
import cn.zn.base.common.utils.ResultUtil;
import cn.zn.role.service.SysRoleDeptService;
import cn.zn.role.service.SysRoleMenuService;
import cn.zn.role.service.SysRoleService;
import cn.zn.role.vo.SysRoleEditVo;
import cn.zn.role.vo.SysRolePageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 角色信息
 *
 * @author zn-admin
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysRoleMenuService roleMenuService;

    @Autowired
    private SysRoleDeptService roleDeptService;

    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/list")
    public ResultData list(SysRolePageVo vo) {
        Object result = roleService.selectRoleList(vo);
        return ResultUtil.success(result);
    }

    /**
     * 根据角色编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:info')")
    @GetMapping(value = "/{roleId}")
    public ResultData getInfo(@PathVariable String roleId) {
        return ResultUtil.success(roleService.getById(roleId));
    }

    /**
     * 新增角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody @Validated SysRoleEditVo vo) {
        roleService.insertRole(vo);
        return ResultUtil.success();

    }

    /**
     * 修改保存角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/{roleId}")
    public ResultData edit(@PathVariable String roleId, @RequestBody @Validated SysRoleEditVo vo) {
        roleService.updateRole(roleId, vo);
        return ResultUtil.success();
    }

    /**
     * 修改保存数据权限
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/dataScope/{roleId}")
    public ResultData dataScope(@PathVariable String roleId, @RequestBody SysRoleEditVo vo) {
        roleService.authDataScope(roleId, vo);
        return ResultUtil.success();
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus/{roleId}")
    public ResultData changeStatus(@PathVariable String roleId, @RequestBody SysRoleEditVo vo) {
        roleService.updateRoleStatus(roleId, vo);
        return ResultUtil.success();
    }

    /**
     * 删除角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public ResultData remove(@PathVariable String[] roleIds) {
        roleService.deleteRoleByIds(roleIds);
        return ResultUtil.success();
    }

    @GetMapping("/selectedMenu/{roleId}")
    public ResultData getSelectedMenu(@PathVariable String roleId) {
        return ResultUtil.success(roleMenuService.selectMenuListByRoleId(roleId));
    }

    @GetMapping("/selectedDept/{roleId}")
    public ResultData getSelectedDept(@PathVariable String roleId) {
        return ResultUtil.success(roleDeptService.selectDeptListByRoleId(roleId));
    }

}
